//
//  AudioPlayProgressView.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/6/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "AudioPlayProgressView.h"
#define LineViewHeight 6
@interface AudioPlayProgressView ()

@property(nonatomic ,strong)UIView *potView;

@property(nonatomic ,strong)UIView *slideView;//滑道 默认高度22pt

@property(nonatomic ,strong)UIView *lineGreenView;//滑道 默认高度22pt

@property(nonatomic ,strong)UIView *lineGray;//滑道 默认高度22pt

@end
@implementation AudioPlayProgressView

- (instancetype)initWithFrame:(CGRect)frame Progress:(double)progress
{
    self = [super initWithFrame:frame];
    [self setUI];
    progress = 0;
    self.lineGreenView.width = progress*(self.slideView.width - 10) + 5;
    self.potView.centerX = progress*(self.slideView.width - 10) + 5;
    return self;
}
- (void)setProgress:(double)progress
{
    _progress = progress;
    if (_progress > 1) {
        _progress = 1;
    }
    self.lineGreenView.width = _progress*(self.slideView.width - 10) + 5;
    self.potView.centerX = _progress*(self.slideView.width - 10) + 5;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    UITouch *touch = [touches anyObject];
//    CGPoint p = [touch locationInView:self.slideView];
//    if(p.x < 5||p.x > self.slideView.width -  5)
//    {
//        return;
//    }
//    if ((p.x - self.lineGreenView.width) != 0) {
//        self.lineGreenView.width =  self.lineGreenView.width + p.x - self.lineGreenView.width;
//        self.potView.centerX = self.lineGreenView.width;
//        if (self.block) {
//            self.block((self.potView.centerX - 5)/(self.slideView.width - 10));
//        }
//    }
}

- (void)setUI
{
    [self addSubview:self.slideView];
    [self.slideView addSubview:self.lineGreenView];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 12)];
    title.textColor = rgba(142, 142, 142, 1);
    title.font = RFontWithSize(12);
    title.text = @"00:04 /  00:51";
    title.textAlignment = NSTextAlignmentLeft;
    [title sizeToFit];
    title.height = 12;
    title.centerY = self.slideView.centerY;
    title.x = self.width - title.width;
    self.timeLabel = title;
    self.slideView.width = self.width - title.width - 10;
    self.lineGreenView.width = self.width - title.width - 10;
    self.slideView.width = self.width - title.width - 10;
    self.lineGray.width = self.width - title.width - 10;
    [self addSubview:title];
    [self.slideView addSubview:self.potView];
}
- (UIView *)potView
{
    if (!_potView) {
        _potView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.slideView.height - 22)/2.0, 22, 22)];
        UIView *linePot = [[UIView alloc] initWithFrame:CGRectMake((22 - 10)/2.0, (22 - 10)/2.0, 10, 10)];
        linePot.backgroundColor = rgba(12, 183, 108, 1);
        [_potView addSubview:linePot];
        [linePot setAllCornersWithCornerRadius:5];
        _potView.userInteractionEnabled = NO;
        _potView.backgroundColor = lClearColor;
    }
    return _potView;
}
- (UIView *)slideView
{
    if (!_slideView) {
        _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.height - 22)/2.0, self.width, 22)];
        _slideView.clipsToBounds = YES;
        self.lineGray =  [[UIView alloc] initWithFrame:CGRectMake(0, (22 - LineViewHeight)/2.0, self.width, LineViewHeight)];
        self.lineGray.backgroundColor = rgba(229, 229, 229, 1);
        
        [_slideView addSubview:self.lineGray];
        [self.lineGray setAllCorner:LineViewHeight/2.0];
        self.lineGray.userInteractionEnabled = NO;
        _slideView.backgroundColor = lClearColor;
    }
    return _slideView;
}
- (UIView *)lineGreenView{
    if (!_lineGreenView) {
     
        _lineGreenView = [[UIView alloc] initWithFrame:CGRectMake(0, (22 - LineViewHeight)/2.0, self.width, LineViewHeight)];
        _lineGreenView.backgroundColor = rgba(12, 183, 108, 1);
        [_lineGreenView setAllCorner:LineViewHeight/2.0];
        _lineGreenView.userInteractionEnabled = NO;
    }
    return _lineGreenView;
}
@end
